/**
 * --------------------------------------------------------------------------
 * Hi.js (v1.0.0): hiloader.js
 * require jquery
 * --------------------------------------------------------------------------
 */
;
(function($, window, document, undefined) {
    "use strict";

    var hiloader = window.hiloader || {};

    var modules = {
        button: {
            js: 'jquery.button.js',
            css: 'button.css'
        },
        tooltip: {
            js: 'jquery.tooltip.js',
            css: 'tooltip.css'
        },
        pagination: {
            js: 'jquery.pagination.js',
            css: 'pagination.css',
            dependencies: ['button']
        },
        parser: {
            js: 'jquery.parser.js'
        }
    };

    var locales = {
        'zh_CN': 'hiui-lang-zh_CN.js'
    }

    var queues = {};

    function loadScript(url, callback) {
        var done = false;
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.language = 'javascript';
        script.src = url;
        script.onload = script.onreadystatechange = function() {
            if (!done && (!script.readyState || script.readyState == 'loaded' || script.readyState == 'complete')) {
                done = true;
                script.onload = script.onreadystatechange = null;
                if (callback) {
                    callback.call(script);
                }
            }
        }
    }

    function runScript(url, callback) {
        loadScript(url, function() {
            document.getElementsByTagName('head')[0].removeChild(this);
            if (callback) {
                callback();
            }
        })
    }

    function loadStyle(url, callback) {
        var link = document.createElement('link');
        link.rel = 'stylesheet';
        link.type = 'text/css';
        link.media = 'screen';
        link.href = url;
        document.getElementsByTagName('head')[0].appendChild(link);
        if (callback) {
            callback.bind(link);
        }
    }

    function loadSingle(name, callback) {
        queues[name] = 'loading';

        var module = modules[name];
        var scriptStatus = 'loading';
        var styleStatus = (hiloader.css && module['css']) ? 'loading' : 'loaded';

        if (hiloader.css && module['css']) {
            var url = hiloader.base + 'themes' + easyloader.theme + '/' + module['css'];
            if (/^http/i.test(module['css'])) {
                url = module['css'];
            }
            loadStyle(url, function() {
                styleStatus = 'loaded';
                if (scriptStatus == 'loaded' && styleStatus == 'loaded') {
                    finish();
                }
            })

            url = hiloader.base + 'modules/' + module['js'];
            if (/^http/i.test(module['js'])) {
                url = module['js'];
            }

            function finish() {
                queues[name] = 'loaded';
                hiloader.onProgress(name);
                if (callback) {
                    callback();
                }
            }
        }
    }

    function loadModule(name, callback) {
        var mm = [];
        var doLoad = false;

        if (typeof name === 'string') {
            add(name);
        } else {
            for (var i = 0; i < name.length; i++) {
                add(name[i]);
            }
        }
        mm.unshift('parser');

        function add(name) {
            if (!modules[name]) return;
            var d = modules[name]['dependencies'];
            if (d) {
                for (var i = 0; i < d.length; i++) {
                    add(d[i]);
                }
            }
            mm.push(name);
        }

        function fnish() {
            if (callback) {
                if (window.jQuery) {
                    window.jQuery.parser.parseVars();
                }
                callback();
            }
            hiloader.onLoad(name);
        }

        var time = 0;

        function loadMM() {
            if (mm.length) {
                var m = mm[0];
                if (!queues[m]) {
                    doLoad = true;
                    loadSingle(m, function() {
                        mm.shift();
                        loadMM();
                    })
                } else if (queues[m] == 'loaded') {
                    if (time < hiloader.timeout) {
                        time += 10;
                        setTimeout(arguments.callee, 10);
                    }
                } else {
                    if (hiloader.locale && doLoad == true && locales[hiloader.locale]) {
                        var url = hiloader.base + 'locale/' + locales[hiloader.locale];
                        runScript(url, function() {
                            finish();
                        })
                    } else {
                        finish();
                    }
                }
            }
        }

        loadMM();
    }

    hiloader = {
        modules: modules,
        locales: locales,

        base: '.',
        theme: 'default',
        css: true,
        locale: null,
        timeout: 2000,

        load: function(name, callback) {
            if (/\.css$/i.test(name)) {
                if (/^http/i.test(name)) {
                    loadStyle(name, callback);
                } else {
                    loadStyle(hiloader.base + name, callback);
                }
            } else if (/\.js$/.test(name)) {
                if (/^http/i.test(name)) {
                    loadScript(name, callback);
                } else {
                    loadScript(hiloader.base + name, callback);
                }
            } else {
                loadModule(name, callback);
            }
        }
    };

    var scripts = document.getElementsByTagName('script');
    for (var i = 0; i < scripts.length; i++) {
        var src = scripts[i].src;
        if (!src) continue;
        var m = src.match(/hiloader\.js(\W|$)/i);
        if (m) {
            hiloader.base = src.substring(0, m.index);
        }
    }

    window.using = hiloader.load;

    if (window.jQuery) {
        jQuery(function() {
            hiloader.load('parser', function() {
                jQuery.parser.parse();
            });
        });
    }

})(jQuery, window, document);